﻿using System.Collections.Generic;
using System.Linq;
using System.Threading;
using CommonBehaviors.Actions;
using Styx;
using Styx.Common;
using Styx.CommonBot;
using Styx.CommonBot.Profiles;
using Styx.Pathing;
using Styx.TreeSharp;
using Styx.WoWInternals;
using Styx.WoWInternals.WoWObjects;

using Action = Styx.TreeSharp.Action;

namespace Honorbuddy.Quest_Behaviors.Hooks
{
	[CustomBehaviorFileName(@"Hooks\TheEndlessFlowHook-Horde")]
	public class TheEndlessFlowHookHorde : CustomForcedBehavior
	{
		public TheEndlessFlowHookHorde(Dictionary<string, string> args)
			: base(args)
		{

				QuestId = 0; //GetAttributeAsQuestId("QuestId", true, null) ?? 0;
 
		}
		public int QuestId { get; set; }
		private bool _isBehaviorDone;
		private Composite _root;
		
		public QuestCompleteRequirement questCompleteRequirement = QuestCompleteRequirement.NotComplete;
		public QuestInLogRequirement questInLogRequirement = QuestInLogRequirement.InLog;
		
		public override bool IsDone
		{
			get
			{
				return inserted;
			}
		}

		private bool inserted = false;
		private LocalPlayer Me
		{
			get { return (StyxWoW.Me); }
		}

		public static WoWUnit Bunny
		{
			get
			{
				return ObjectManager.GetObjectsOfType<WoWUnit>().Where(r => r.Entry == 44360).OrderBy(r=>r.Distance2D).FirstOrDefault();
			}
		}

		public static WoWUnit Stickbone
		{
			get
			{
				return ObjectManager.GetObjectsOfType<WoWUnit>().Where(r => r.Entry == 44329 && r.IsAlive).OrderBy(r => r.Distance2D).FirstOrDefault();
			}
		}

		public static WoWItem Bomb
		{
			get
			{
				return StyxWoW.Me.BagItems.FirstOrDefault(r => r.Entry == 60678);
			}
		}

		private Composite myHook;
		public override void OnStart()
		{
			OnStart_HandleAttributeProblem();
			
			if (myHook == null)
			{
				myHook = new PrioritySelector(
					//Remove hook once were done with the quest
					new Decorator(r=> Bomb == null, new Action(r=>TreeHooks.Instance.RemoveHook("Questbot_Main", myHook))),
					new Decorator(r=> Bunny != null && Bunny.Distance < 35 && Bunny.Distance > 5, new Action(r=>Navigator.MoveTo(Bunny.Location))),

					new Decorator(r=> Bunny != null && Bunny.Distance <=5, new Sequence(
						new Action(ret => Bomb.Use()),
						new WaitContinue(1, ret => Me.CurrentPendingCursorSpell != null, new ActionAlwaysSucceed()),
						new Action(ret => SpellManager.ClickRemoteLocation(Bunny.Location)))),

					new Decorator(r => Stickbone != null && Stickbone.Distance <= 5, new Sequence(
						new Action(ret => Bomb.Use()),
						new WaitContinue(1, ret => Me.CurrentPendingCursorSpell != null, new ActionAlwaysSucceed()),
						new Action(ret => SpellManager.ClickRemoteLocation(Stickbone.Location)))));
				TreeHooks.Instance.InsertHook("Questbot_Main", 0, myHook);
			}
			else
			{
				TreeHooks.Instance.RemoveHook("Questbot_Main", myHook);
			}
			inserted = true;
		}

		public bool IsQuestComplete()
		{
			var quest = StyxWoW.Me.QuestLog.GetQuestById((uint)QuestId);
			return quest == null || quest.IsCompleted;
		}
	
		public Composite DoneYet
		{
			get
			{
				return
					new Decorator(ret => IsQuestComplete(), new Action(delegate
					{
						TreeRoot.StatusText = "Finished!";
						_isBehaviorDone = true;
						return RunStatus.Success;
					}));
			}
		}
	}
}
